package com.lin.csdn.service.impl;

import com.lin.csdn.dao.impl.BlogDao;
import com.lin.csdn.dao.impl.BrowseDao;
import com.lin.csdn.dao.impl.UserDao;
import com.lin.csdn.service.intf.IBlogService;
import com.lin.csdn.utils.BaseLogger;
import com.lin.csdn.utils.DateView;
import com.lin.csdn.utils.QrcodeUtils;
import com.lin.csdn.utils.ResultSplit;

import java.io.File;
import java.io.IOException;
import java.util.List;

public class BlogService implements IBlogService {

    BlogDao blogDao=new BlogDao();
    BrowseDao browseDao=new BrowseDao();
    UserDao userDao=new UserDao();
    DateView dateView=new DateView();
    ResultSplit split=new ResultSplit();
    QrcodeUtils qrcodeUtils=new QrcodeUtils();
    BaseLogger baseLogger=new BaseLogger();

    @Override
    public List<Object> blog(String domain,int beginIndex,int pageSize) {
        Object result=blogDao.search(domain,beginIndex,pageSize);
        return split.resultSplit(result);
    }

    //复杂查询
    public List<Object> seek(String target){
        Object result=blogDao.seek(target);
        return split.resultSplit(result);
    }

    //根据点赞数排序
    public List<Object> thumbOrder(){
        Object result=blogDao.thumbOrder();
        return split.resultSplit(result);
    }

    @Override
    public List<Object> ownBlog(String loginName,int beginIndex,int pageSize){
        int userId=userDao.findId(loginName);
        Object result=blogDao.ownBlog(userId,beginIndex,pageSize);
        return split.resultSplit(result);
    }

    @Override
    public List<Object> idBlog(String blogId){
        Object result=blogDao.idBlog(Integer.parseInt(blogId.replace(" ","")));
        return split.resultSplit(result);
    }

    @Override
    public String addBlog(String userName){
        int userId=userDao.findId(userName);
        String date=dateView.getDate();
        int blogId=blogDao.addBlog(userName,userId,date);
        return String.valueOf(blogId);
    }

    //添加文章文本url信息
    public void addArticle(String blogId){
        blogDao.addNewArticle(Integer.parseInt(blogId.replace(" ","")));
    }

    //获取文章id
    public boolean getArticleId(String blogId){
        String result=blogDao.getArticleId(Integer.parseInt(blogId.replace(" ","")));
        return result.equals("404");
    }

    //更新状态，用于上传博客
    public void updateStatus(String blogId, String searchPath){
        String path;
        path = qrcodeUtils.creatQrcode(searchPath,Integer.parseInt(blogId.replace(" ","")));
        //转换成二维码，并返回储存的相对路径
        blogDao.updateStatus(Integer.parseInt(blogId.replace(" ", "")), path);
    }

    @Override
    public void deleteBlog(String blogId){
        String url=browseDao.getArticle(Integer.parseInt(blogId.replace(" ","")));
        //首先获取文件的路径
        File file=new File(url);
        if(file.exists()) {
            file.delete();//删除文本文件
        }
        browseDao.deleteArticle(Integer.parseInt(blogId.replace(" ","")));
        blogDao.deleteBlog(Integer.parseInt(blogId.replace(" ","")));
    }

    //点赞功能
    public void addThumb(String blogId){
        String oldPraise=blogDao.loadThumb(Integer.parseInt(blogId.replace(" ","")));
        //读取旧的点赞数
        int newPraise=Integer.parseInt(oldPraise)+1;
        //加一后重新更新新的点赞数
        blogDao.addThumb(newPraise,Integer.parseInt(blogId.replace(" ","")));
    }
}
